import AES from "crypto-js/aes";
import UTF8 from "crypto-js/enc-utf8";
import { getGlobalEnv } from "./constants";

const { cryptoSecret } = getGlobalEnv();

export function encrypt<T = any>(text: T): string {
  return AES.encrypt(JSON.stringify(text), cryptoSecret).toString();
}

export function decrypt<T = any>(cipher: string): T | string {
  return JSON.parse(AES.decrypt(cipher, cryptoSecret).toString(UTF8));
}
